package com.estore.model;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.io.Serializable;
/**
 * 商品实体类
 */
//public class Product implements Comparable<Product>{
public class Product implements Serializable  {

    private String id;      // 商品ID
    private String name;    // 商品名称
    private double price;   // 价格
    private int stock;      // 库存
    private Set<String> tags;
    public Product(String id, String name, double price, int stock,Set<String>tags) {
        this.id = id;
        this.name = name;
        this.price = price;
        this.stock = stock;
        this.tags=tags !=null ? tags:new HashSet<>();
    }

    // Getters and Setters ...

    public Set<String> getTags() {
        return tags;
    }

    public void setTags(Set<String> tags) { this.tags=tags; }
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public double getPrice() { return price; }
    public void setPrice(double price) { this.price = price; }
    public int getStock() { return stock; }
    public void setStock(int stock) { this.stock = stock; }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Product{id=").append(id)
                .append(", name='").append(name).append('\'')
                .append(", price=").append(price);

        if (tags != null && !tags.isEmpty()) {
            sb.append(" | 标签: [");
            // 遍历 tags 拼接成 "[标签1, 标签2, ...]" 格式
            int i = 0;
            for (String tag : tags) {
                if (i > 0) {
                    sb.append(", ");
                }
                sb.append(tag);
                i++;
            }
            sb.append("]");
        }
        sb.append('}');
        return sb.toString();
    }

    // 重写 equals 和 hashCode 是一个好习惯，特别是当对象要放入Set或作为Map的key时
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Product product = (Product) o;
        return Objects.equals(id, product.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

//    @Override
//    public int compareTo(Product other) {
//        return this.id.compareTo(other.id);
//    }
}
